package com.stellar.circle.domain.sChatSensitiveLog.impl;

import com.stellar.circle.basic.entity.SChatMessage;
import com.stellar.circle.basic.entity.SChatSensitiveLog;
import com.stellar.circle.basic.service.SChatMessageService;
import com.stellar.circle.basic.service.SChatSensitiveLogService;
import com.stellar.circle.controller.admin.sChatSensitiveLog.vo.SChatSensitiveLogRequestPageVO;
import com.stellar.circle.controller.admin.sChatSensitiveLog.vo.SChatSensitiveLogRequestVO;
import com.stellar.circle.controller.admin.sChatSensitiveLog.vo.SChatSensitiveLogResponseVO;
import com.stellar.circle.convert.SChatSensitiveLogConvert;
import com.stellar.circle.domain.sChatSensitiveLog.SChatSensitiveLogDomainService;
import com.stellar.web.core.bean.PageResult;
import com.stellar.web.core.enums.IsDeletedFlagEnum;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static com.stellar.circle.constants.CircleConstants.IS_HIDDEN_YES;
import static com.stellar.circle.constants.CircleConstants.IS_READ_NO;

/**
 * 聊天敏感词记录表领域服务实现
 *
 * @author Jqchan
 * @version 1.0
 * @date 2025-06-12 21:21:52
 */
@Service
public class SChatSensitiveLogDomainServiceImpl implements SChatSensitiveLogDomainService {

    @Resource
    private SChatSensitiveLogService sChatSensitiveLogService;

    @Resource
    private SChatMessageService sChatMessageService;

    @Override
    public PageResult<SChatSensitiveLogResponseVO> page(SChatSensitiveLogRequestPageVO requestPageVO) {
        PageResult<SChatSensitiveLogResponseVO> pageResult = new PageResult<>();
        SChatSensitiveLog queryEntity = SChatSensitiveLogConvert.INSTANCE.convert(requestPageVO);
        queryEntity.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getVal());

        List<SChatSensitiveLog> entities = sChatSensitiveLogService.page(queryEntity,
                requestPageVO.getStart(), requestPageVO.getPageSize());

        if (entities.isEmpty()) {
            return pageResult;
        }

        Long count = sChatSensitiveLogService.count(queryEntity);
        List<SChatSensitiveLogResponseVO> vos = SChatSensitiveLogConvert.INSTANCE.convertEntityToRespList(entities);

        pageResult.setResult(vos);
        pageResult.setTotal(count.intValue());
        return pageResult;
    }

    @Override
    public Boolean update(SChatSensitiveLogRequestVO requestVO) {
        SChatSensitiveLog entity = SChatSensitiveLogConvert.INSTANCE.convert(requestVO);
        return sChatSensitiveLogService.update(entity) != 0;
    }

    @Override
    public Boolean delete(Long id) {
        SChatSensitiveLog entity = new SChatSensitiveLog();
        entity.setId(id);
        entity.setIsDeleted(IsDeletedFlagEnum.DELETED.getVal());
        return sChatSensitiveLogService.update(entity) != 0;
    }

    @Override
    public Boolean insert(SChatSensitiveLogRequestVO requestVO) {
        SChatSensitiveLog entity = SChatSensitiveLogConvert.INSTANCE.convert(requestVO);
        return sChatSensitiveLogService.insert(entity) != 0;
    }

    @Override
    public SChatSensitiveLogResponseVO get(Long id) {
        SChatSensitiveLog entity = sChatSensitiveLogService.queryById(id);
        return SChatSensitiveLogConvert.INSTANCE.convert(entity);
    }

    @Override
    public void handleSensitiveMessage(Long userId, SChatMessage message, List<String> words) {
        SChatMessage sChatMessage = new SChatMessage();
        sChatMessage.setSenderId(userId);
        sChatMessage.setReceiverId(message.getReceiverId());
        message.setContentType(message.getContentType());
        sChatMessage.setIsHidden(IS_HIDDEN_YES);
        message.setHiddenReason("敏感词过滤");
        sChatMessage.setIsRead(IS_READ_NO);
        sChatMessage.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getVal());
        sChatMessageService.insert(sChatMessage);
    }
}
